package org.liveboardgames.agricola.control.delegates
{
	import com.adobe.cairngorm.business.ServiceLocator;

	import flash.utils.setTimeout;

	import mx.collections.ListCollectionView;
	import mx.rpc.IResponder;

	import org.liveboardgames.agricola.control.delegates.action.AgricolaActionController;
	import org.liveboardgames.agricola.control.delegates.mock.GameFactory;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.vo.Game;

	public class CreateGameDelegate
	{
		private var _responder:IResponder;

		private var _service:Object;

		public function CreateGameDelegate(responder:IResponder)
		{
			this._service = ServiceLocator.getInstance().getRemoteObject("createGameService");
			this._responder = responder;
		}

		public function createGame(players:ListCollectionView, gameType:String):void
		{
//			setTimeout(createLocalGame, 0, players);
			var call:Object = _service.createGame(players, gameType);
			call.resultHandler = _responder.result;
			call.faultHandler = _responder.fault;
		}

		private function createLocalGame(players:ListCollectionView):void
		{
			var newGame:Game = GameFactory.createGame(players);
			AgricolaModelLocator.getInstance().game = newGame;
			AgricolaModelLocator.getInstance().game.hasPlayerPlayedPeep = true;
			AgricolaModelLocator.getInstance().actionController = new AgricolaActionController(newGame);
		}
	}
}